<?php 

class ControleBanner {
	

	public function cadastrar(Banner $banner) {
		
		include_once("../conexao/config.php");
		$conexao = new Config();
		
		$query = "insert into banner values(NULL, ?, ?)";
		
		$stmt = $conexao->conectar()->prepare($query);
		
		$stmt->bindValue(1, $banner->getBanner());
		$stmt->bindValue(2, $banner->getAtivado());
		
		$stmt->execute();		
	}
	
	
	public function totalDeBannersAtivos() {
		
		include_once("../conexao/config.php");
		$conexao = new Config();
		
		$query = "select count(*) as total_banner from banner where ativado_banner=1";
		
		$stmt->bindValue(1, $id);
		
		$stmt->execute();
		
		return $stmt;
	}
	
	public function remover($id) {
		
		include_once("../conexao/config.php");		
		$config = new Config();
		$conexao = $config->conectar();
		
		$query_caminho = "select foto_banner from banner where id_banner = ?";
		
		$caminho = $conexao->prepare($query_caminho);
		
		$caminho->bindValue(1, $id);
		
		$caminho->execute();
		
		if($row = $caminho->fetch()) {
			$banner = $row['foto_banner'];
			//remover o banner da pasta
			unlink($banner);
		}
		
		$query_delete = "delete from banner where id_banner = ?";
		
		$stmt = $conexao->prepare($query_delete);
		
		$stmt->bindValue(1, $id);
		
		$stmt->execute();
		
		$meta = '<meta http-equiv="refresh" content="0, URL=banners" />';
		echo $meta;		
	}
	
	public function totalDeBanner() {
		
		include_once("../conexao/config.php");
		$conexao = new Config();
		
		$query = "select * from banner";	
		
		$stmt = $conexao->conectar()->prepare($query);
		
		$stmt->execute();		
		
		return $stmt->rowCount(PDO::FETCH_ASSOC);
	}
	
	public function pesquisar() {
		
		include_once("../conexao/config.php");
		$conexao = new Config();
		
		$query = "select * from banner";	
		
		$stmt = $conexao->conectar()->prepare($query);
		
		$stmt->execute();
		
		return $stmt;		
	}
	
	public function ativarDesativar($id) {
		
		include_once("../conexao/config.php");		
		$config = new Config();
		$conexao = $config->conectar();
		
		$query = "select ativado_banner from banner where id_banner = ?";
		$query2 = "update banner set ativado_banner = ? where id_banner = ?";
		
		$stmt = $conexao->prepare($query);
		
		$stmt->bindValue(1, $id);
		
		$stmt->execute();
		
		if($row = $stmt->fetch()) {
			$banner = $row['ativado_banner'];
			
			if($banner == '1') {
				$stmt2 = $conexao->prepare($query2);
				
				$stmt2->bindValue(1, 0);
				$stmt2->bindValue(2,  $id);
				
				$stmt2->execute();
			} else {
				$stmt2 = $conexao->prepare($query2);
				
				$stmt2->bindValue(1, 1);
				$stmt2->bindValue(2,  $id);
				
				$stmt2->execute();
			}
		}				
		
		$meta = '<meta http-equiv="refresh" content="0, URL=banners" />';
		echo $meta;		
	}
	
}
?>